/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.gui.dialogs;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Shell;
import org.toedo.gui.translations.Messages;
import org.toedo.model.Task;

public class DateSelectorDialog extends TitleAreaDialog {

	private DateTime mDate;
	
	private Calendar mInitialCalendar = null;
	private Date mSelectedDate = null;
	
	private Task mTask;
	
	public DateSelectorDialog(Shell parentShell, Task task, Date currentDate) {
		super(parentShell);
		mTask = task;
		mSelectedDate = currentDate;
		mInitialCalendar = new GregorianCalendar();
		mInitialCalendar.setTime(currentDate);
	}
	
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		
		return contents;
	}
	
	protected Control createDialogArea(Composite parent) {
		setTitle(Messages.DateSelectorDialog_Title);
		if (mTask != null) {
			setMessage(String.format(Messages.DateSelectorDialog_Message, mTask.getTitle()));
		}
		
		Composite composite = (Composite) super.createDialogArea(parent);
		
		composite.setLayout(new GridLayout(1, false));
		
		mDate = new DateTime (composite, SWT.CALENDAR | SWT.BORDER);
		mDate.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));		

		mDate.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected (SelectionEvent e) {
				updateDate();
			}
		});
		
		setDate(mSelectedDate);
		
		return parent;
	}
	
	private void updateDate() {
		Calendar cal = new GregorianCalendar();
		cal.set(mDate.getYear(), mDate.getMonth(), mDate.getDay());
		
		cal.set(Calendar.HOUR_OF_DAY, mInitialCalendar.get(Calendar.HOUR_OF_DAY));
		cal.set(Calendar.MINUTE, mInitialCalendar.get(Calendar.MINUTE));
		cal.set(Calendar.SECOND, mInitialCalendar.get(Calendar.SECOND));
		cal.set(Calendar.MILLISECOND, mInitialCalendar.get(Calendar.MILLISECOND));
		
		mSelectedDate = cal.getTime();
	}
	
	private void setDate(Date date) {
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);
	
		mDate.setDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
	}
	
	public Date getSelectedDate() {	
		return mSelectedDate;
	}

}